/* Author: Sebastian Qvarfordt
*/


(function($) {
  $.fn.damn_load = function(url, template, emptyText) {
    var elements = this;
    elements.addClass('loading');
    $.getJSON(url, function(data){
      return elements.each(function() {
        if (data.length) {
          //$(template).tmpl(data).appendTo($(this));
          $(this).html($(template).tmpl(data));
        } else {
          $(this).html(emptyText);
        }
        $(this).removeClass('loading');
      });
    });
  };
})(jQuery);

(function($) {
  $.fn.damn_click = function(success_func) {
    this.live('click', function() { var e=$(this); $.get(e.attr('href'), function(data) { success_func(e, data); }); return false; });
  };
})(jQuery);

$(function() {
	function generateNoise(opacity) { //Thanks nettuts
		if ( !!!document.createElement('canvas').getContext )
			return false;

		var canvas = document.createElement("canvas"),
			ctx = canvas.getContext('2d'),
			x, y,
			number,
			opacity = opacity || .2;

		canvas.width = 45;
		canvas.height = 45;

		for ( x = 0; x < canvas.width; x++ ) {
			for ( y = 0; y < canvas.height; y++ ) {
				number = Math.floor( Math.random() * 60 );

				ctx.fillStyle = "rgba(" + number + "," + number + "," + number + "," + opacity + ")";
				ctx.fillRect(x, y, 1, 1);
			}
		}

		document.body.style.backgroundImage = "url(" + canvas.toDataURL("image/png") + ")";
	}

	function get_assets(q, offset, limit) {
		var getData = { "search": q, "start": offset, "end": offset+limit };
		var assets
		assets = $.ajax({
			url: "/api/assets/search",
			type: "GET",
			data: getData,
			dataType: "json",
			success: function(data) {
				var results = $("<div>");
				$.each(data, function(key, result) {
					var link = $("<a>", { "class": "search_result_lnk", "href": "/assets/"+result.pk, "title": result.fields.subName })
						.html("<img class='model_preview small' src='/api/assets/" + result.pk + "/transcode/?format=image/png&sizex=128&sizey=128' />")
						.appendTo(results);
				});

				$("#main .right .search_results").append(results.html());
				
				count = assets.getResponseHeader("Assets-Total-Count");

				if($(document).height() - $(window).height() == 0 && count > offset) {
					get_assets(q, offset, offset+30);
				}
			}
		});
	}

	function clear_results() {
		$("#main .right .search_results").empty();
	}

	//INIT
	//get_assets("", 0, 30);
	//generateNoise(.15);
	var q = "";
	var offset = 0;

	$(document).bind("scroll", function() {
		if($(document).height() - ($(window).height()+$(document).scrollTop()) < 20) {
			offset += 30;
			//get_assets(q, offset, offset+30);
		}
	});

	//EVENTS
	$("#asset_search_form .search_btn").bind("click", function() {
		clear_results();
		offset = 0;
		q = $(this).siblings(".search_input").val();
		get_assets(q, offset, 30);
		return false;
	});

	$("#header div.menu li, .contribute").hover(function() {
		$(this).find("img").animate({ opacity: 1 }, 200);
	}, function() {
		$(this).find("img").animate({ opacity: 0.7 }, 200);
	});
});
